package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.model.entity.common.CommonPlugIn;
import com.settlement.system.model.entity.common.TblWpAuditCloseAccount;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.reqparam.OrderCloseAccountReqParam;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpAuditCloseAccountService;
import com.settlement.system.service.IWpPlugInService;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 唯品稽核关账设置 前端控制器
 *
 * @author xiaochuan
 * @since 2020-11-19
 */

@Slf4j
@Api(tags = "唯品稽核关账设置", description = "唯品稽核关账设置")
@RestController
@RequestMapping("/wp/audit/closeAccount")
public class TblWpAuditCloseAccountController extends ExcelController<TblWpAuditCloseAccount> {

    @Resource
    private ITblWpAuditCloseAccountService tblWpAuditCloseAccountService;

    @Resource
    private IWpPlugInService wpPlugInService;

    @Override
    public IBaseService init() {
        return tblWpAuditCloseAccountService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpAuditCloseAccount.class;
    }

    @Override
    protected ObjectDataResponse insert(@RequestBody TblWpAuditCloseAccount model, HttpServletRequest request) {
        String error = verifyImportObject(model, null);
        if (!StringUtils.isEmpty(error)) {
            return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), error);
        }
        setDefualt(model);
        try {
            tblWpAuditCloseAccountService.insert(model);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_SUCCESS.getValue());
        } catch (Exception ex) {
            if (!StringUtils.isEmpty(ex.getMessage())) {
                return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue(), ex.getMessage());
            }
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @Override
    public String verifyImportObject(TblWpAuditCloseAccount closeAccount, AnalysisContext analysisContext) {
        String error = super.verifyImportObject(closeAccount, analysisContext);
        if (!StringUtils.isEmpty(error)) {
            return error;
        }

        HashMap<String, Object> params = new HashMap<>(16);
        params.put("shopNo", closeAccount.getShopNo());
        try {
            List<CommonPlugIn> commonPlugIns = wpPlugInService.getShop(params);
            if (CollectionUtils.isEmpty(commonPlugIns)) {
                return String.format("店铺编码:%s在WP-商户配置中不存在", closeAccount.getShopNo());
            }
            closeAccount.setShopName(commonPlugIns.get(0).getName());
        } catch (Exception ex) {
            log.error("获取唯品锁帐店铺异常", ex);
            return "系统异常";
        }

        if (closeAccount.getRealCloseAccountDate().compareTo(closeAccount.getInitialDate()) < 0) {
            return "关账日期不能小于初始日期";
        }

        closeAccount.setNeedRollbackAccountData(closeAccount.getInitialDate());
        closeAccount.setValidCloseAccountDate(closeAccount.getInitialDate());
        return null;
    }

    @PostMapping("closeAccount")
    public ObjectDataResponse closeAccount(@RequestBody OrderCloseAccountReqParam reqParam) {
        try {
            if (CollectionUtils.isEmpty(reqParam.getIds())) {
                return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), "请选择需要锁帐的数据");
            }

            if (reqParam.getRealCloseAccountDate() == null) {
                return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), "请选择需要锁帐的日期");
            }

            tblWpAuditCloseAccountService.closeAccount(reqParam.getIds(), reqParam.getRealCloseAccountDate());

        } catch (Exception ex) {
            log.error("锁帐异常", ex);
            new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue(), ex.getMessage());
        }
        return new ObjectDataResponse(ReturnStatusEnum.REQUEST_SUCCESS.getValue(), "更新成功");
    }

    @Override
    protected ObjectDataResponse delete(@RequestBody TblWpAuditCloseAccount model, HttpServletRequest request) {
        try {
            String error = null;
            TblWpAuditCloseAccount closeAccount = tblWpAuditCloseAccountService.getById(model.getId());
            if (closeAccount == null) {
                error = "关账数据不存在";
            }
            if (closeAccount != null && closeAccount.getInitialDate().compareTo(closeAccount.getRealCloseAccountDate()) != 0) {
                error = "初始日期不等于关账日期,不能删除";
            }

            if (!StringUtils.isEmpty(error)) {
                return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), error);
            }

            tblWpAuditCloseAccountService.deleteClose(closeAccount);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_SUCCESS.getValue(), "删除成功");
        } catch (Exception ex) {
            log.info("删除数据异常", ex);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue());
        }
    }
}

